
#include <BOpenGL>
#include "member_BGeometry.h"
#include "member_BElement.h"

using namespace BWE;

member_BGeometry::member_BGeometry(BGeometry* geometry)
{
	boss = geometry;
}
member_BGeometry::~member_BGeometry()
{

}

void member_BGeometry::drawPiecesLObject(BElement* element, GLenum mode)
{
	if (element_member(element)->dirtyList)
	{
		BPieceArray* pieces = element->pieces();

		BColorArray* cs = colors;
		BVectorArray* vs = vertices;
		BVectorArray* ns = normals;
		BCoordArray* ts = coords[0];

		if (element_member(element)->listName == 0)
		{
			GLuint listName = glGenLists(1);
			element_member(element)->listName = listName;
		}

		GLuint listName = element_member(element)->listName;

		glNewList(listName, GL_COMPILE);
		glBegin(mode);
		for (int i = 0; i < pieces->size(); i++)
		{
			const BPiece& piece = (*pieces)[i];
			if (cs)
			{
				const BColor& color = (*cs)[piece.v()];
				glColor3ubv((GLubyte*)&color);
			}
			if (ns)
			{
				const BVector& normal = (*ns)[piece.vn()];
				glNormal3fv((GLfloat*)&normal);
			}
			if (ts)
			{
				const BCoord& coord = (*ts)[piece.vt()];
				glTexCoord2fv((GLfloat*)&coord);
			}
			const BVector& vertex = (*vs)[piece.v()];
			glVertex3fv((GLfloat*)&vertex);
		}
		glEnd();
		glEndList();

		element_member(element)->dirtyList = false;
	}
	GLuint listName = element_member(element)->listName;
	if (listName > 0)
		glCallList(listName);
}
